<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of notify_obj
 *
 * @author Juraj
 */
class notify_obj {

    //put your code here
    public $email;

    public function __construct() {
        include( PATH_TO_LIBRARY . "phpmailer/class.phpmailer.php");
        $this->email = new phpmailer();
        $this->email->CharSet = "utf-8";
        $this->email->WordWrap = 50;

        //$this->email->Host = "smtp.websupport.sk";
        //$this->email->Mailer = "smtp";
        //$this->email->Password = "Dohodnisi0246";
        //$this->email->Username = "dohodnisi@dohodnisi.sk";
        //$this->email->SMTPAuth = "true";
    }

    public function send($adressTo, $message, $subject = "", $from = array(), $isHtml = true, $isAllow = false) {

        $this->email->IsHTML($isHtml);
        $this->email->From = NOTIFY_EMAIL;
        $this->email->FromName = NOTIFY_USER;
        $this->email->Subject = NOTIFY_SUBJECT;

        base_obj::debug("notif from", $from);
        base_obj::debug("notif subject", $subject);

        if (!empty($from['email'])) {
            $this->email->From = $from['email'];
        }
        if (!empty($from['name'])) {
            $this->email->FromName = $from['name'];
        }
        if (!empty($subject)) {
            $this->email->Subject = $subject;
        }

        if (!empty($adressTo)) {
            if (is_array($adressTo)) {
                foreach ($adressTo as $key => $email) {
                    $this->email->AddAddress($email);
                }
            } else {
                $this->email->AddAddress($adressTo);
            }

            $this->email->Body = $message;

            $status = EMAIL_NOT_SENT;
            $result = false;

            if (ALLOW_EMAIL || $isAllow) {
                if ($this->email->Send()) {
                    $status = EMAIL_SENT;
                    $result = true;
                }
            } else {
                $status = EMAIL_DB_SENT;
                $result = true;
                mail("jpogadl@gmail.com", 'CM Vypnute emaily', "Bol odoslany email. Ale system je odstaveny.");
            }

            $in_data = array(
                "recipient" => json_encode($adressTo),
                "date" => time(),
                "subject" => $this->email->Subject,
                "message" => $message,
                "status" => $status,
                "sender_email" => $this->email->From,
                "sender_name" => $this->email->FromName
            );

            base_obj::debug('notify data', $in_data);
            
            $res = $this->add_notification($in_data);
            if (error_obj::isError($res))
                return $res;

            return $result;
        }
        return new error_obj(EMAIL_ERROR_EMPTY_ADDRESS, __CLASS__, __FUNCTION__);
    }

    private function add_notification($in_data) {
        $db_obj = new db_obj();
        $res = $db_obj->insert($GLOBALS['tb_notifications'], $in_data);
        if (error_obj::isError($res))
            return $res;
        return true;
    }

}

?>
